KEYPAD INTERFACING WITH AVR
4x4 Keypad consists of 4 rows and 4 columns (Matrix). Switches are placed between the rows and columns. In this project, we will learn How to interface a 4X4 keypad with AVR ATmega16 microcontroller. 
Synopsis

Keypads are parts of HMI or Human Machine Interface and play really important role in a small embedded system where human interaction or human input is needed. Matrix keypads are well known for their simple architecture and ease of interfacing. In this project, we will learn How to interface a 4X4 keypad with AVR ATmega16 microcontroller.

Description

AVR ATmega16 microcontroller is a 16K Bytes of In-System Self-programmable Flash memory.

Program Flow of ATmega16 microcontroller

A special register called the program counter keeps track of the address in the Flash RAM to be executed.

On each clock cycle, that address is decoded and the appropriate values are taken from memory and passed into the Arithmetic Logic Unit (ALU) to be processed Unit.

The value to be processed (operands) must come from a special set of 32 registers called the General Purpose Registers. To access SRAM or I/O, they must be brought in/out of the registers.

Microcontrollers uses oscillator (a quartz crystal) which can produce a constant square wave to provide the synchronization of everything on the chip. Each rising edge of the square wave allows an operation to take place such as loading instructions executing place such as loading instructions, executing instructions, and saving results. The Atmega16 can be set to have a clock frequency of 8MHz.

Keypad is used as an input device to read the key pressed by user and to process it.

4x4 Keypad consists of 4 rows and 4 columns (Matrix). Switches are placed between the rows and columns. A key press establishes a connection between corresponding row and column between which the switch is placed.

In order to read the key press we need to configure the rows as outputs and columns as inputs.

Columns are read after applying signals to the rows in order to determine whether or not a key is pressed and if pressed, which key is pressed.

The matrices are actually an interface technique. According to this technique, I/O’s are divided into two sections: the columns and the rows. You can imagine a matrix as an excel sheet. Here is a 4 x 4 matrix. The A, B, C and D are the columns and the 1, 2, 3 and 4 are the rows. There are 16 knots that the rows and columns intersect. To make a keypad matrix, we will have to connect a switch to each knot. The switch will have a push-to-make contact. When the operator pushes this switch, it will connect the column and the row that it corresponds to.

Proteus design for Keypad interfacing with AVR


Orcad design for Keypad interfacing with AVR


Keypad interfacing with AVR

/*  Name     : Main.c
 *  Purpose  : Source code for keypad Interfacing with ATMEGA16.
 *  Author   : GEMICATES
 *  Date     : 04-07-2017
 *  Website  : www.gemicates.org
 *  Revision : None
 */

#include<avr/io.h>				// Header File for ATMEGA16
#include<util/delay.h>			        // Include Delay Function

	#define KEY PORTB		        // To Set Port B as Keypad

	#define r1 PB0			        // To Set Pin B0-B3 as Keypad Row			
	#define r2 PB1
	#define r3 PB2
	#define r4 PB3
 
	#define c1 PB4   		        // To Set Pin B4-B6 as Keypad Column	
	#define c2 PB5
	#define c3 PB6

/* Function to Check Four Rows and Columns*/
void check1(void);
void check2(void);  
void check3(void);
void check4(void);
 
	#define LCD_DATA PORTD		        // To Set Port D as LCD data Pins
 
	#define CON PORTA			// To Set Port A Pins A0-A2 as LCD Control Pins 

	#define en PA2				// Enable signal
	#define rw PA1				// Read/Write signal
	#define rs PA0				// Resister Select signal
 
/* Function For LCD display*/
void LCD_cmd(unsigned char cmd);
void init_LCD(void);
void LCD_write(unsigned char data);
void string(unsigned char *str);
 
/* Main Function*/

void main()
{
	unsigned char value; 
 
	DDRD=0xff;			        // LCD_DATA port as output port
	DDRA=0x07;			        // Set the Data Direction Register (A2-A0) as Ouput
	DDRB=0x0F;			        // Set the Data Direction Register (B3-B0) as Ouput
	KEY=0xf0;			        // Make Input Pins are High
	init_LCD();			        // initialization of LCD
    LCD_cmd(0x86);  	                        // bring cursor to position 6 of ROW 1	
	LCD_write('H');
	LCD_cmd(0x87);  	                // bring cursor to position 7 of ROW 1													
	LCD_write('I');
	LCD_write('!');
	_delay_ms(50);
	LCD_cmd(0xC3);		                // bring cursor to position 3 of ROW 2
	string("**GUYS**");
	_delay_ms(50);
	LCD_cmd(0x01);  		        // clear screen
				
	LCD_cmd(0x83);			        // bring cursor to position 3 of ROW 1
	string("WELCOME TO");
	_delay_ms(100);
	LCD_cmd(0xC3);			        // bring cursor to position 3 of ROW 2
	string("GEMICATES");
	_delay_ms(100);
	LCD_cmd(0x01);		 	        // clear screen
	LCD_cmd(0x0C);  	                // Display On cursor Off
	string("press a key");
	LCD_cmd(0xc0);
 
 
	while(1)
	{
		PORTB=0xF0;			// Set all the input to one
		value=PINB;			// Get the PORTD value in variable "value"
		if(value!=0xf0)		        // If any key is pressed value changed
		{
			check1();
			check2();
			check3();
			check4();
		}
	}
	return 0;
}
 
void check1(void)			        // To check Row1
{
	KEY =0b11111110;
	_delay_ms(20);
	if(bit_is_clear(PINB,c1))
	{
	output(1);
	_delay_ms(20);
	}
	else if(bit_is_clear(PINB,c2))
	{
	output(2);
	_delay_ms(20);
	}
	else if(bit_is_clear(PINB,c3))
	{
	output(3);
	_delay_ms(20);
	}
}

 
 
void check2(void)			        // TO check Row2
{
	KEY=0b11111101;
	_delay_ms(20);
	if(bit_is_clear(PINB,c1))
	{
	output(4);
	_delay_ms(20);
	}
	else if(bit_is_clear(PINB,c2))
	{
	output(5);
	_delay_ms(20);
	}
	else if(bit_is_clear(PINB,c3))
	{
	output(6);
	_delay_ms(20);
	}
}
 
void check3(void)				// To check Row3
{
	KEY=0b11111011;
	_delay_ms(20);
	if(bit_is_clear(PINB,c1))
	{
	output(7);
	_delay_ms(20);
	}
	else if(bit_is_clear(PINB,c2))
	{
	output(8);
	_delay_ms(20);
	}
	else if(bit_is_clear(PINB,c3))
	{
	output(9);
	_delay_ms(20);
	}
 }
void check4(void)				// To check Row4
{
	KEY =0b11110111;
	_delay_ms(20);
	if(bit_is_clear(PINB,c1))
	{
	output(10);
	_delay_ms(20);
	}
	else if(bit_is_clear(PINB,c2))
	{
	output(11);
	_delay_ms(20);
	}
	else if(bit_is_clear(PINB,c3))
	{
	output(12);
	_delay_ms(20);
	}
} 
void init_LCD(void)
{
 
	LCD_cmd(0x38);		                // initializtion of 16X2 LCD in 8bit mode
	_delay_ms(1);
 
	LCD_cmd(0x01);		                // clear LCD
	_delay_ms(1);
 
	LCD_cmd(0x0E);		                // cursor ON
	_delay_ms(1);
 
	LCD_cmd(0x80);		                // ---8 go to first line and --0 is for 0th position
	_delay_ms(1);
	return;
}
 
void LCD_cmd(unsigned char cmd)
{
	LCD_DATA=cmd;
	CON =(0<<rs)|(0<<rw)|(1<<en);	        // making RS and RW as LOW and EN as HIGH
	_delay_ms(1);
	CON =(0<<rs)|(0<<rw)|(0<<en);         	// making RS, RW , LOW and EN as LOW
	_delay_ms(50);
	return;
}
 
 
void LCD_write(unsigned char data)
{
	LCD_DATA= data;
	CON = (1<<rs)|(0<<rw)|(1<<en);	        // making RW as LOW and RS, EN as HIGH
	_delay_ms(1);
	CON = (1<<rs)|(0<<rw)|(0<<en);	        // making EN and RW as LOW and RS HIGH
	_delay_ms(50);				// give a 10 milli second delay to get thigs executed
	return ;
}
 
void string(unsigned char *str)	                // take address vaue of the string in pionter *str
{
	int i=0;
	while(str[i]!='\0')			// loop will go on till the NULL charaters is soon in string 
	{
		LCD_write(str[i]);		// sending data on CD byte by byte
		i++;
	}
	return;
}

void output(int rev)
{
	LCD_cmd(0x01);				// clear screen
	LCD_cmd(0x86);				// bring cursor to position 3 of ROW 1
	switch(rev)
	{
		case 1:
			string("ONE");
			_delay_ms(1000);
		break;
		
		case 2:
			string("TWO");
			_delay_ms(1000);
		break;
		
		case 3:
			string("THREE");
			_delay_ms(1000);
		break;
		
		case 4:
			string("FOUR");
			_delay_ms(1000);
		break;
		
		case 5:
			string("FIVE");
			_delay_ms(1000);
		break;
		
		case 6:
			string("SIX");
			_delay_ms(1000);
		break;
		
		case 7:
			string("SEVEN");
			_delay_ms(1000);
		break;
		
		case 8:
			string("EIGHT");
			_delay_ms(1000);
		break;
		
		case 9:
			string("NINE");
			_delay_ms(1000);
		break;
		
		case 10:
			string("TEN");
			_delay_ms(1000);
		break;
		
		case 11:
			string("ELEVEN");
			_delay_ms(1000);

		break;
		
		case 12:
			string("TWELVE");
			_delay_ms(1000);
		break;
	}
}

Error message here!

Show Error message here!


Forgot your password?

Error message here!

Send OTP

Error message here!

Show Error message here!


Lost your password? Please enter your email address. You will receive a password you Need.

Send Error message here!


Back to log-in

Close